import {getcheckList} from "../util/api.js";

function getState(userId){
	getcheckList({},userId).then((res)=>{
		if(res.code == 200){
			if(res.data){
				if(res.data.startworktime!=null){
					return 1;
				}
				if(res.data.endworktime!=null){
					return 2;
				}
			}else{
				return 0;
			}
		}else{
			return null;
			uni.showToast({
				title:'查询统计时间失败',
				icon:none,
			})
		}
	}).catch((err)=>{})
}

function  getDateRange(dateNow,intervalDays,bolPasTime){
		let oneDayTime =  24*60*60*1000;
		let list = [];
		let lastDay;
		if(bolPasTime == true){
			lastDay = new Date(dateNow.getTime()  - intervalDays*oneDayTime);
			list.push(formateDate(lastDay));
			list.push(formateDate(dateNow));
		}else{
			lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);
			list.push(formateDate(dateNow));
			list.push(formateDate(lastDay));
		}
			return list;
};
//效验8-12位数字
function regxNumber(num){
	var reg = /^[0-9A-Za-z]{8,12}$/;
	var m = reg.test(num);
	return m;
}

function fliterNum1(e){
	var msg;
	switch (e) {
		case '0101':
		msg = '待核实';break
		case '0102':
		msg = '待核实';break
		case '0103':
		msg = '待转办';break
		case '0104':
		msg = '已回退';break
		case '0201':
		msg = '派遣中';break
		case '0202':
		msg = '待处置';break
		case '0203':
		msg = '待回转';break
		case '0205':
		msg = '待审核';break
		case '0206':
		msg = '待核查';break
		case '0207':
		msg = '待核查';break
		case '0208':
		msg = '待结案';break
		case '0209':
		msg = '结案';break
		case '0001':
		msg = '销案';break
		case '0002':
		msg = '销案';break
	}
	return msg;
}
function fliteState(e){
	
}

function  formateDate(time){
	let year =  time.getFullYear()
	let month = time.getMonth()+1
	let day = time.getDate()
	if(month<10){
		month = '0' +month
	}
	if(day<10){
		day = '0'+day
	}
	return year+ '-'+month+'-'+day+''
}

function  getYear(){
	var time = new Date()
	let year =  time.getFullYear()
	let month = time.getMonth()+1
	let day = time.getDate()
	if(month<10){
		month = '0' +month
	}
	if(day<10){
		day = '0'+day
	}
	return year+ '年'+month+'月'+day+'日'
}

function  getTimeNow(){
	var date = new Date(),
	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
	minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
	second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	var timer =hour + ':' + minute + ':' + second;
	return timer;
}

function  getTimeMinte(){
	var date = new Date(),
	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
	minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
	second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	var timer =hour + ':' + minute;
	return timer;
}
function getTodayTime(){
	var date = new Date(),
	year = date.getFullYear(),
	month = date.getMonth() + 1,
	day = date.getDate(),
	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
	minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
	second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	return timer;
}

// 图片转换为base64：H5和App
function detailImage(path, callback) {
	// #ifdef APP-PLUS
	plus.io.resolveLocalFileSystemURL(path, function(entry) {
		entry.file(function(file) {
			var fileReader = new plus.io.FileReader();
			//alert("getFile:" + JSON.stringify(file));
			fileReader.readAsDataURL(file);
			fileReader.onloadend = function(evt) {
				callback(evt.target.result);
			}
		})
	})
	// #endif


	// #ifdef H5
	var img = new Image();
	img.src = path;
	img.onload = function() {
		//默认按比例压缩
		var w = this.width,
			h = this.height;
		var quality = 0.95; // 默认图片质量为0.7

		//生成canvas
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');

		// 创建属性节点
		canvas.setAttribute("width", w);
		canvas.setAttribute("height", h);

		ctx.drawImage(this, 0, 0, w, h);
		// quality值越小，所绘制出的图像越模糊
		var base64 = canvas.toDataURL('image/jpeg', quality);
		// 回调函数返回base64的值
		callback(base64);
	};
	// #endif
}
let rad = (d) => {
	return d * Math.PI / 180.0;
}

function getDistance(lat1, lng1, lat2, lng2){
	let radLat1 = rad(lat1);
	let radLat2 = rad(lat2);
	let a = radLat1 - radLat2;
	let b = rad(lng1) - rad(lng2);
	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));

	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; //输出为公里

	let distance = s;
	let distance_str = "";

	if (parseInt(distance) >= 1) {
		distance_str = distance.toFixed(1) + "km";
	} else {
		distance_str = distance * 1000 + "m";
	}

	return s;
}

//比较时间大小 开始时间  结束时间
function timeCakel(date1,date2){
	var oDate1 = new Date(date1);
	var oDate2 = new Date(date2);
	if(oDate1.getTime() > oDate2.getTime()){
		return true
	} else  if(oDate1.getTime() < oDate2.getTime()){
		return false
	}else if(oDate1.getTime() == oDate2.getTime()){
		return false
	}
}
module.exports = {
    getDateRange,
	getTodayTime,
	regxNumber,
	detailImage,
	fliterNum1,
	fliteState,
	getDistance,
	timeCakel,
	getYear,
	getTimeNow,
	getTimeMinte,
	getState
}